<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\statechange;



class WalletController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}





	/**
	 * 钱包账户管理
	 **/
	public function wallet_list(Request $request) {
		$condition = array();
		$pursetype = $request->input('pursetype');
		if(!empty($pursetype)){
			$pursetype = explode('_', $pursetype);
		}
		$ownertype = $request->input('ownertype');
		if(!empty($ownertype)){
			$ownertype = explode('_', $ownertype);
		}

		$nodecode = $request->input('nodecode');
		if(!empty($nodecode)){
			$condition[] = ['nodecode','=',$nodecode];
		}
		$nodename = $request->input('nodename');
		if(!empty($nodename)){
			$condition[] = ['nodename','=',$nodename];
		}

		$purseid = $request->input('purseid');
		if(!empty($purseid)){
			$condition[] = ['purseid','=',$purseid];
		}

		
		if(!empty($pursetype)){
			$condition[] = ['pursetype','=',$pursetype[0]];
		}
		if(!empty($ownertype)){
			$condition[] = ['ownertype','=',$ownertype[0]];
		}
	
		
		
		//查询钱包转账
			$data = DB::table('tblc_user_purse t')
			->leftJoin('tnet_reginfo r', 't.ownerid', '=', 'r.nodeid')
			->leftJoin('tblc_currency c', 't.currencytype', '=', 'c.currencyid')
			->select('t.purseid','t.ownerid','t.pursetype','t.subid','t.balance',DB::raw('t.minvalue'),'t.ownertype',
					't.currencytype','t.hisid_last','t.status','t.create_time','t.freezevalue','r.nodeid','r.nodecode','r.nodename','c.currencyid','c.currency_type','c.currency_unit')
			->where($condition)
			->orderBy('t.balance','DESC')
			->paginate();
			foreach ($data as $key => &$value) {
				$value->nstatus = explode(',',$value->status);
				unset($value->status);

			}
		//展示模板
		return view('admin.wallet.wallet_list',[
			'data' => $data,
			'nodecode' => $nodecode,
			'nodename' => $nodename,
			'purseid' => $purseid,
			'ownertype' => $ownertype[1],
			'pursetype' => $pursetype[1]
		]);
	}


	/**
	 * 创建钱包账户
	 **/
	public function add_wallet(Request $request) {
		     	$id = $request->input('ruleid');
		    	if($request->isMethod('post')){
				$from_ownertype = $request->input('from_ownertype');
				if(!$from_ownertype){
					$this->ajax_error('请选择转出方类型');
				}

				$data = array(
					'ruleid' => Basiconfig::getNextSeq(),
					'from_ownertype' => $request->input('from_ownertype'), //转出方类型
					'from_pursetype' => $request->input('from_pursetype'), //转出方钱包类型
					'to_ownertype' => $request->input('to_ownertype'), //接收方类型
					'to_pursetype' => $request->input('to_pursetype'), //接收方钱包类型
					'state' => $request->input('state'), //状态
					'same_ownerid' => $request->input('same_ownerid'),//是否
					'same_subid' => $request->input('same_subid'),//是否
					'remark' => $request->input('remark'), //备注

				);
				if(!$id){

					$result = Db::table('tblc_transfer_rule')->insert($data);
					if(!$result){
						$this->ajax_error('添加失败');
					}
						// record_action(AdminAction::getNextSeq(),'rbac','menuAdd','菜单信息添加',json_encode($_POST));
						$this->ajax_success('添加成功');
				}else{
					$data['ruleid'] = $id;
					$result1 = Db::table('tblc_transfer_rule')->where('ruleid',$id)->update($data);
					if($result1){
						$this->ajax_success('编辑成功');
					}
				}
			
		
		}

				$condition = array();
				if($id>0){
					$condition[] = ['ruleid','=',$id];
				}else{
					$condition[] = ['ruleid','=',''];
				}
				$list =  Db::table('tblc_transfer_rule')->where($condition)->first();
				//货币类型
				$currency = Db::table('tblc_currency')->get();
				//展示模板
				return view('admin.wallet.add_wallet',[
				 'action'=>'add_rule',
				 'data'=>$list,
				 'currency'=>$currency,
				 'id'=>$id
				]);
	}
	
	/**
	 * 冻结资金
	 **/
	public function block_money(Request $request) {
		     	$purseid = $request->input('purseid');
		    	if($request->isMethod('post')){
		    	$purseid = $request->input('purseid');
				$freezevalue = $request->input('freezevalue');
				$remarks = $request->input('remarks');

				if($purseid){
					$freezemoney = DB::table('tblc_user_purse')->where('purseid', $purseid)->value('freezevalue');
					$update['freezevalue'] = $freezemoney+$freezevalue;
					DB::table('tblc_user_purse')->where('purseid', $purseid)->update($update);
					$this->ajax_success('冻结成功');
					
				}else{
					$this->ajax_error('冻结失败');
				}
			
		
		}
				//展示模板
				return view('admin.wallet.block_money',[
				 'action'=>'block_money',
				 'purseid'=>$purseid
				]);
	}

	/**
	 * 变动历史
	 */
	public function change_history(Request $request){
		$purseid = $request->input('purseid');
		$data = $data = DB::table('tblc_user_purse_his')->where('purseid',$purseid)->orderBy('amount','desc')->paginate(10);
		//展示模板
        return view('admin.wallet.change_history',['data'=>$data]);
	}
	/**
	 * 流水统计
	 */
	public function flow_statistics(Request $request){
		$purseid = $request->input('purseid');
		$condition = array();
		$conditionout = array();
		$condition[] = ['out_purse_id','=',$purseid];
		$conditionout[] = ['into_purse_id','=',$purseid];
		//出账
		$outdata = DB::table('tblc_transfer t')
		->leftJoin('tblc_transfer_reson r','t.reason' , '=' , 'r.reson_code')
		->leftJoin('tblc_user_purse p','p.purseid' , '=' , 't.out_purse_id')
		->leftJoin('tblc_currency c','p.currencytype' , '=' , 'c.currencyid')
		->select('t.reason','t.out_amount','t.out_purse_id','r.reson_name','c.currency_unit',DB::raw('count(*) as aaa'),DB::raw('SUM(out_amount) as bbb'))
		->groupBy('t.reason','t.out_amount','t.out_purse_id','r.reson_name','c.currency_unit')
		->where($condition)
		->paginate(10);
		$outdata->appends($request->all());
		
		//出账总计
		$out_count = '';
		foreach ($outdata as $k => &$v) {
			unset($v->out_amount);
			$out_count  += $v->bbb;
		}
		$out_count = $out_count<=0 ? 0 :  $out_count;
		//入账
		$intodata = DB::table('tblc_transfer f')
		->leftJoin('tblc_transfer_reson r','f.reason' , '=' , 'r.reson_code')
		->leftJoin('tblc_user_purse p','p.purseid' , '=' , 'f.into_purse_id')
		->leftJoin('tblc_currency c','p.currencytype' , '=' , 'c.currencyid')
		->select('f.reason','f.into_amount','f.into_purse_id','r.reson_name','c.currency_unit',DB::raw('count(*) as aaa'),DB::raw('SUM(into_amount) as bbb'))
		->groupBy('f.reason','f.into_amount','f.into_purse_id','r.reson_name','c.currency_unit')
		->where($conditionout)
		->paginate(10);


		$intodata->appends($request->all());
		//dd($intodata);
		//入账总计
		$into_count = '';
		foreach ($intodata as $key => &$value) {
			unset($value->into_amount);
			$into_count  += $value->bbb;
		}
		$into_count = $into_count<=0 ? 0 :  $into_count;
		//平账
		$p_count = $into_count-$out_count;
		//展示模板
        return view('admin.wallet.flow_statistics',[
        	'outdata'=>$outdata,
        	'intodata'=>$intodata,
        	'into_count'=>$into_count,
        	'out_count'=>$out_count,
        	'p_count'=>$p_count,
       ]);
	}

	/**
	 * 锁定/解锁
	 **/
	public function lock_unlock(Request $request) {
		     	$purseid = $request->input('purseid');
		    	if($request->isMethod('post')){
		    	$purseid = $request->input('purseid');
		    	$rules = $request->input('rules');

	            if(!empty($rules)){
	                $rules = implode(',',$rules).',';
	            }
				$remarks = $request->input('remarks');
				$datas = DB::table('tblc_user_purse')->where('purseid', $purseid)->first();
				
				//状态变更历史记录
				$insert = array(
					'lock_id' => Statechange::getNextSeq(),
					'purse_id' => $purseid, //ID
					'before_status' => $datas->status, //锁定前状态
					'after_status' =>$rules, //锁定后状态
					'op_node_id' => $_SESSION['admin_uid'], //操作员ID
					'remarks' => $remarks, //锁定备注
				);
				Db::table('tblc_purse_lock')->insert($insert);
				if($purseid){
					// $update['remarks'] =$remarks;
					$update['status'] =$rules;
					DB::table('tblc_user_purse')->where('purseid', $purseid)->update($update);
					$this->ajax_success('操作成功');
					
				}else{
					$this->ajax_error('操作失败');
				}
			
		
		}
			   $list =  Db::table('tblc_user_purse')->where('purseid',$purseid)->first();
			   $list->nstatus = explode(',',$list->status);
			   unset($list->status);
			   // dd( $list);
				//展示模板
				return view('admin.wallet.lock_unlock',[
				 'action'=>'lock_unlock',
				 'list'=>$list,
				 'purseid'=>$purseid
				]);
	}

	/**
	 * 状态变更历史
	 **/
	public function statechange(Request $request) {
		$condition = array();
		$lockid = $request->input('lockid');
		if(!empty($lockid)){
			$condition[] = ['lock_id','=',$lockid];
		}
		$operatorcode = $request->input('operatorcode');
		if(!empty($operatorcode)){
			$condition[] = ["r.nodecode",'=',$operatorcode];
		}

		
		// if(!empty($pursetype)){
		// 	$condition[] = ['pursetype','=',$pursetype[0]];
		// }
		// if(!empty($ownertype)){
		// 	$condition[] = ['ownertype','=',$ownertype[0]];
		// }
	
		
		
		//查询账户状态变更历史
			$data = DB::table('tblc_purse_lock c')
			->leftJoin('tblc_user_purse t', 't.purseid','=','c.purse_id')
			//操作员
			->leftJoin('tnet_reginfo r', 'c.op_node_id', '=', 'r.nodeid')
			//拥有者
			->leftJoin('tnet_reginfo i', 'i.nodeid', '=', 't.ownerid')
			
			->select('c.lock_id','c.op_node_id','c.create_time','c.before_status','c.after_status','c.purse_id',
					'c.remarks','t.purseid','t.ownerid','t.pursetype','t.ownertype','r.nodeid',
					'r.nodecode as operatorcode','r.nodename as operatorname','i.nodecode as ownercode','i.nodename as ownername')
			->where($condition)
			->orderBy('c.create_time','DESC')
			->paginate(10);
			//dd($data);
			foreach ($data as $key => &$value) {
				$value->bstatus = explode(',',$value->before_status);
				$value->astatus = explode(',',$value->after_status);
				unset($value->before_status);
				unset($value->after_status);

			}
		//展示模板
		return view('admin.wallet.statechange',[
			'data' => $data,
			'lock_id' => $lockid
			
		]);
	}

	/**
	 * 资金冻结历史
	 **/
	public function fundfreeze(Request $request) {
		$condition = array();
		$pursetype = $request->input('pursetype');
		if(!empty($pursetype)){
			$pursetype = explode('_', $pursetype);
		}
		$ownertype = $request->input('ownertype');
		if(!empty($ownertype)){
			$ownertype = explode('_', $ownertype);
		}

		$nodecode = $request->input('nodecode');
		if(!empty($nodecode)){
			$condition[] = ['nodecode','=',$nodecode];
		}

		$hisid = $request->input('hisid');
		if(!empty($hisid)){
			$condition[] = ['hisid','=',$hisid];
		}

		$purseid = $request->input('purseid');
		if(!empty($purseid)){
			$condition[] = ['h.purseid','=',$purseid];
		}

		$ownerid = $request->input('ownerid');
		if(!empty($ownerid)){
			$condition[] = ['ownerid','=',$ownerid];
		}
		$state = $request->input('state');

		if(!empty($state)){
			$condition[] = ['state','=',$state];
		}
		
		if(!empty($pursetype)){
			$condition[] = ['pursetype','=',$pursetype[0]];
		}
		if(!empty($ownertype)){
			$condition[] = ['ownertype','=',$ownertype[0]];
		}
	
		
		
		//查询冻结记录
			$data = DB::table('tblc_freeze_his h')
			->leftJoin('tblc_user_purse p', 'h.purseid', '=', 'p.purseid')
			->leftJoin('tblc_currency c', 'h.currencytype', '=', 'c.currencyid')
			->leftJoin('tnet_reginfo r', 'p.ownerid', '=', 'r.nodeid')
			->select('h.hisid','h.purseid','h.amount','h.createtime','h.remarks','h.opremarks','h.state','h.optime','h.transferid','p.ownerid','p.pursetype','p.ownertype','c.currency_type','r.nodecode','r.nodename')
			->where($condition)
			->orderBy('h.amount','DESC')
			->paginate(10);
		  // dd($data);
		//展示模板
		return view('admin.wallet.fundfreeze',[
			'data' => $data,
			'nodecode' => $nodecode,
			'hisid' => $hisid,
			'purseid' => $purseid,
			'ownerid' => $ownerid,
			'state' => $state,
			'ownertype' => $ownertype[1],
			'pursetype' => $pursetype[1]
		]);
	}
	/**
	 * 解冻资金
	 **/
	public function unfreeze_funds(Request $request) {
		     	$hisid = $request->input('hisid');
		    	if($request->isMethod('post')){
		    	$hisid = $request->input('hisid');
		    	$state = DB::table('tblc_freeze_his')->where('hisid', $hisid)->value('state');
		    	if($state==1){
		    		$this->ajax_error('已处理');
		    	}
				$update['opremarks'] = $request->input('opremarks');
				$update['optime'] = date('Y-m-d H:i:s',time());
				$update['state'] = 1;
				if($hisid){
					DB::table('tblc_freeze_his')->where('hisid', $hisid)->update($update);
					$this->ajax_success('解冻成功');
				}else{
					$this->ajax_error('解冻失败');
				}
			
		
			}
			$condition = array();
			$condition[] = ['hisid','=',$hisid];
			//解冻的钱包
			$data = DB::table('tblc_freeze_his h')
			->leftJoin('tblc_user_purse p', 'h.purseid', '=', 'p.purseid')
			->leftJoin('tblc_currency c', 'h.currencytype', '=', 'c.currencyid')
			->leftJoin('tnet_reginfo r', 'p.ownerid', '=', 'r.nodeid')
			->select('h.hisid','h.purseid','h.amount','h.createtime','h.remarks','h.opremarks','h.state','h.optime','h.transferid','p.ownerid','p.pursetype','p.ownertype','c.currency_type','r.nodecode','r.nodename')
			->where($condition)
			->first();

				//展示模板
				return view('admin.wallet.unfreeze_funds',[
				 'data'	=>$data,
				 'action'=>'unfreeze_funds',
				 'hisid'=>$hisid
				]);
	}


}